<?php

namespace App\Common\Util\ImitateAop\Attributes;

use App\Common\Util\ImitateAop\Aspects\LockAspect;
use Sc\Util\ImitateAopProxy\Interfaces\ImitateAspectAttrInterface;
use Sc\Util\ImitateAopProxy\Interfaces\ImitateAspectInterface;

/**
 * 锁切片注解
 *
 * Class Lock
 */
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
class Lock implements ImitateAspectAttrInterface
{
    /**
     * @param string $lockSign 锁得签名，以 {} 包围得值取函数调用参数，例：签名包含函数调用id参数 sign-key-{id}
     *                         a($id, $d)    => a(17, 'test')
     *                         sign-key-{id} => sign-key-17
     * @param string $failMessage 遇锁阻塞时提示语
     */
    public function __construct(public string $lockSign, public string $failMessage = '人太多啦，再试试', public int $waitTime = 0)
    {}

    /**
     * @param \ReflectionFunctionAbstract $reflectionFunctionAbstract
     *
     * @return ImitateAspectInterface
     */
    public function getImitateAspect(\ReflectionFunctionAbstract $reflectionFunctionAbstract): ImitateAspectInterface
    {
        return new LockAspect($this->lockSign, $this->failMessage, $this->waitTime, $reflectionFunctionAbstract);
    }
}